package dao;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 * This class implements tables that I can stick result set queries into.
 * 
 *@author Lee
 *@version 1.0
 */
public class ResultSetTable extends JTable
{
	private final DefaultTableModel dataModel;
	private boolean select;
	
	/**
	 * Constructor for the result set table.
	 * 
	 * @param rs
	 * @param isSearch
	 * @param isCat
	 * @param select
	 * @param isResults
	 */
	public ResultSetTable(ResultSet rs, boolean isSearch, boolean isCat, boolean select, boolean isResults) {

		super();
		dataModel = new DefaultTableModel();
		setModel(dataModel);
		this.select=select;

		try {
			ResultSetMetaData mdata = rs.getMetaData();
			int colCount = mdata.getColumnCount();
			String[] colNames = new String[colCount];
			for (int i = 1; i <= colCount; i++) {
				colNames[i - 1] = mdata.getColumnName(i);
			}
			dataModel.setColumnIdentifiers(colNames);
			if (isSearch)
			{
				String[] columnID = {" ID", "Item Name", "Current Bid", 
						"High Bidder", "Get It Now", "Auction Ends"};
				dataModel.addRow(columnID);
			}
			
			if (isResults)
			{
				String[] columnID = {"ID", "Item Name", "Sale Price", "Winner", "Auction Ended"};
				dataModel.addRow(columnID);
			}

			if (isCat)
			{
				String[] columnID = {"Category", "Total Items", "Min Price", 
						"Max Price", "Average Price"};
				dataModel.addRow(columnID);
			}
			
			if (select)
			{
				String[] columnID = {"Username", "Listed", "Sold", "Purchased", "Rated"};
				dataModel.addRow(columnID);
			}

			while (rs.next()) {
				String[] rowData = new String[colCount];
				for (int i = 1; i <= colCount; i++) {
					rowData[i - 1] = rs.getString(i);
				}
				dataModel.addRow(rowData);
			}
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			try {
				rs.close();
			}
			catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}